/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoggingIn.java
 *
 * Created on 13-nov-2011, 18:03:53
 */
/**
 *
 * @author Usuario
 */
import javax.swing.*;
import java.util.Arrays;

public class LoggingIn extends javax.swing.JFrame {

    private static String OK = "Ingresar";
    private static String NOT = "Cancelar";
    
    private JFrame controllingFrame;
    
    /** Creates new form LoggingIn */
    public LoggingIn() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        PasswordField = new javax.swing.JPasswordField();
        SubmitPasswordButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();

        setTitle("Inicio de sesión");

        jLabel1.setFont(new java.awt.Font("Lucida Console", 1, 11));
        jLabel1.setText("Ingrese su clave maestra");

        SubmitPasswordButton.setText("Ingresar");
        SubmitPasswordButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SubmitPasswordButtonActionPerformed(evt);
            }
        });

        CancelButton.setText("Cancelar");
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(100, 100, 100)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(PasswordField, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(14, 14, 14)
                            .addComponent(SubmitPasswordButton)
                            .addGap(18, 18, 18)
                            .addComponent(CancelButton))))
                .addGap(111, 111, 111))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(109, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(PasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SubmitPasswordButton)
                    .addComponent(CancelButton))
                .addGap(100, 100, 100))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_CancelButtonActionPerformed

    private void SubmitPasswordButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SubmitPasswordButtonActionPerformed
        // TODO add your handling code here:
        String cmd = evt.getActionCommand();
        
        if(OK.equals(cmd)){
            char[] passc = PasswordField.getPassword();
            if(validatingPassword(passc)){
                Dashboard dashbi = new Dashboard();
                dashbi.setLocationRelativeTo(null);
                dashbi.setVisible(true);
                this.setVisible(false);
            }
            else{
                JOptionPane.showMessageDialog(controllingFrame,
                    "Incorrecto. Inténtelo de nuevo.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        
    }//GEN-LAST:event_SubmitPasswordButtonActionPerformed

    public boolean validatingPassword(char[] passc){
        boolean isCorrect = true;
        char[] correctPassword = {'m','a','n','n','y'};
        if(passc.length!=correctPassword.length){
            isCorrect = false;
        }
        else{
            isCorrect = Arrays.equals(passc, correctPassword);
        }
        return isCorrect;
        
    }
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LoggingIn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LoggingIn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LoggingIn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LoggingIn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new LoggingIn().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JPasswordField PasswordField;
    private javax.swing.JButton SubmitPasswordButton;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
}
